#
# Copyright (c) 2021 Airbyte, Inc., all rights reserved.
#


import json
import os
import pkgutil
from collections import defaultdict
from typing import Dict, Generator

from airbyte_protocol import AirbyteCatalog, AirbyteConnectionStatus, AirbyteMessage, ConfiguredAirbyteCatalog, ConnectorSpecification

from .logger import AirbyteLogger


class AirbyteSpec(object):
    @staticmethod
    def from_file(file: str):
        with open(file) as file:
            spec_text = file.read()
        return AirbyteSpec(spec_text)

    def __init__(self, spec_string):
        self.spec_string = spec_string


class Integration(object):

    # can be overridden to change an input config
    def configure(self, config: json, temp_dir: str) -> json:
        """
        Persist config in temporary directory to run the Source job
        """
        config_path = os.path.join(temp_dir, "config.json")
        self.write_config(config, config_path)
        return config

    @staticmethod
    def read_config(config_path: str) -> json:
        with open(config_path, "r") as file:
            contents = file.read()
        return json.loads(contents)

    @staticmethod
    def write_config(config: json, config_path: str):
        with open(config_path, "w") as fh:
            fh.write(json.dumps(config))

    # can be overridden to change an input catalog
    def read_catalog(self, catalog_path: str) -> ConfiguredAirbyteCatalog:
        return ConfiguredAirbyteCatalog.parse_obj(self.read_config(catalog_path))

    # can be overridden to change an input state
    def read_state(self, state_path: str) -> Dict[str, any]:
        if state_path:
            state_obj = json.loads(open(state_path, "r").read())
        else:
            state_obj = {}
        state = defaultdict(dict, state_obj)
        return state

    def spec(self, logger: AirbyteLogger) -> ConnectorSpecification:
        """
        Returns the spec for this integration. The spec is a JSON-Schema object describing the required configurations (e.g: username and password)
        required to run this integration.
        """
        raw_spec = pkgutil.get_data(self.__class__.__module__.split(".")[0], "spec.json")
        return ConnectorSpecification.parse_obj(json.loads(raw_spec))

    def check(self, logger: AirbyteLogger, config: json) -> AirbyteConnectionStatus:
        """
        Tests if the input configuration can be used to successfully connect to the integration e.g: if a provided Stripe API token can be used to connect
        to the Stripe API.
        """
        raise Exception("Not Implemented")

    def discover(self, logger: AirbyteLogger, config: json) -> AirbyteCatalog:
        """
        Returns an AirbyteCatalog representing the available streams and fields in this integration. For example, given valid credentials to a
        Postgres database, returns an Airbyte catalog where each postgres table is a stream, and each table column is a field.
        """
        raise Exception("Not Implemented")


class Source(Integration):
    def __init__(self):
        super().__init__()

    def read(
        self, logger: AirbyteLogger, config: json, catalog: ConfiguredAirbyteCatalog, state_path: Dict[str, any]
    ) -> Generator[AirbyteMessage, None, None]:
        """
        Returns a generator of the AirbyteMessages generated by reading the source with the given configuration, catalog, and state.
        """
        raise Exception("Not Implemented")


class Destination(Integration):
    def __init__(self):
        super().__init__()
